<?php
/**
 * 文章管理控制器
 * 
 * @author sandy
 *
 */
class articleApp extends adminControl{
	
	protected function _init(){
		$this->lang->load('book');
	}
	
	/**
	 * 图书文章列表
	 */
	public function index(){
		
		$book_id = isset($_GET['bid']) ? intval($_GET['bid']) : 0;
		
		$article_model = &get_model('article');
		$book_model = &get_model('book');
		$book_info = $book_model->get_book_info($book_id);
		
		// 获取当前页码及分页配置
		$page_info = get_page(20);
		$article = $article_model->get_book_article($book_id,$page_info);
		
		// 获取分页信息
		$page_info['total'] = $article['total'];
		$url = "index.php?c=article&bid=$book_id";
		$pages_data = format_page($page_info['total'], $page_info['cur_page'], $url, $page_info['per_page'],5);
		$pages = get_page_html($pages_data);
		
		$this->assign('article',$article['result']);
		$this->assign('book_info',$book_info);
		$this->assign('pages',$pages);
		
		$this->_title = $this->lang->get('html_title') . ' - ' . $this->lang->get('app_name');
		$this->display();
	}
	
	/**
	 * 编辑图书
	 */
	public function edit(){
		$dosubmit = isset($_REQUEST['dosubmit']) ? $_REQUEST['dosubmit'] : false;
		
		if ($dosubmit){
			$book_id = trim($_POST['book_id']);
			$article_id = trim($_POST['article_id']);
			$content= trim($_POST['content']);
			$article_title = $_POST['article_title'];
				
			$article_info = array(
					'article_id' => $article_id,
					'article_title' => $article_title,
					'content' => $content
			);
			
			$article_mod = &get_model('article');
			$return_url = $this->config->get_item('site_url').'/index.php?c=article&bid='.$book_id;
				
			if($article_mod->update_article($article_info)){
				$this->show_message( "编辑文章成功!",$return_url);
			} else {
				$this->show_message("编辑文章失败!",$return_url);
			}
			
		} else {
			$article_id = isset($_GET['aid']) ? intval($_GET['aid']) : 0;
			$chapter_id = isset($_GET['cid']) ? intval($_GET['cid']) : 0;
			
			$article_mod = &get_model('article');
			$book_model = &get_model('book');
			
			$article_info = $article_mod->get_article_info($article_id);
			$book_info = $book_model->get_book_info($article_info['book_id']);
			
			//如果是章回体形式的，需要读取章节信息
			if ($chapter_id > 0) {
				$chapter_mod = &get_model('chapter');
				$chapter_info = $chapter_mod->get_chapter($chapter_id);
				$this->assign('chapter_info',$chapter_info);
			}
			
			$this->assign('article_info',$article_info);
			$this->assign('book_info',$book_info);
			$this->display();
		}
		
	}
	
	/**
	 * 为图书添加文章
	 */
	public function add(){
		
		$dosubmit = isset($_REQUEST['dosubmit']) ? $_REQUEST['dosubmit'] : false;
		
		if ($dosubmit){
			$book_id = isset($_POST['book_id']) ? intval($_POST['book_id']) : 0;
			$chapter_id = isset($_POST['chapter_id']) ? intval($_POST['chapter_id']) : 0;
			$content= isset($_POST['content']) ? trim($_POST['content']) : 0;
			$article_title = isset($_POST['article_title']) ? trim($_POST['article_title']) : 0;
			
			$article_info = array(
					'article_title' => $article_title,
					'book_id' => $book_id,
					'chapter_id' => $chapter_id,
					'content' => $content
			);	
			
			$article_mod = &get_model('article');
				
			$return_url = $this->config->get_item('site_url').'/index.php?c=article&bid='.$book_id;
			
			
			if($article_mod->add_article($article_info)){
				$this->show_message( "添加文章成功!",$return_url);
			} else {
				$this->show_message("添加文章失败!",$return_url);
			}
				
		} else {
			
			$book_id = isset($_GET['bid']) ? intval($_GET['bid']) : 0;
			$chapter_id = isset($_GET['cid']) ? intval($_GET['cid']) : 0;
			
			$book_model = &get_model('book');
			$book_info = $book_model->get_book_info($book_id);
			
			//如果是章回体形式的，需要读取章节信息
			if ($chapter_id > 0) {
				$chapter_mod = &get_model('chapter');
				$chapter_info = $chapter_mod->get_chapter($chapter_id);
				$this->assign('chapter_info',$chapter_info);
			}
			
			$this->assign('book_info',$book_info);
			$this->display();
		}
	}
	
	/**
	 * 删除指定文章
	 */
	public function del(){
		$article_id= isset($_GET['aid']) ? intval($_GET['aid']) : 0;
		$book_id = isset($_GET['bid']) ? intval($_GET['bid']) : 0;
		
		$article_mod = &get_model('article');
		
		$return_url = $this->config->get_item('site_url').'/index.php?c=article&bid='.$book_id;
		if($article_mod->del_article($article_id)){
			$this->show_message( "删除文章成功!",$return_url);
		} else {
			$this->show_message( "删除文章失败!",$return_url);
		}
	}
}

